const computedBehavior = require("miniprogram-computed").behavior;
Component({
behaviors: [ computedBehavior],
  computed:{
    // 计算是否全选
    allchecked(data){
     return data.list.every(item=>{
       return item.checked == true
      })
    },
    apple(data){
    return data.list.reduce((total,item)=>{
        if(item.checked){
          return (total+=item.minPrice*item.count)
        }
        return total
      },0)
    }
  },
  data: {
    list:[],
    
  },
  methods: {
    countChange(e){
      console.log(e)
      let detail = e.detail
      let idx = e.currentTarget.dataset.idx
      this.data.list[idx].count = detail
      this.setData({
        list:this.data.list
      })
      wx.setStorageSync('cart', this.data.list)
    },
    del(e){
      let idx = e.currentTarget.dataset.idx
      this.data.list.splice(idx,1)
      wx.setStorageSync('cart', this.data.list)
      this.setData({
        list:this.data.list
      })
    },
    // 全选
    quan(){
      let all = !this.data.allchecked
      this.data.list.forEach(item=>{
        item.checked=all
      })
      this.setData({
        list:this.data.list
      })
    },
    // 单选
    danchecked(e){
      let idx = e.currentTarget.dataset.index
      this.data.list[idx].checked = !this.data.list[idx].checked
      this.setData({
        list:this.data.list
      })
      wx.setStorageSync('cart', this.data.list)
    }
  },
  pageLifetimes:{
    show(){
      this.list= wx.getStorageSync('cart')
      this.setData({
        list:this.list
      })
    }
  }
})
